import { defineStore } from "pinia";
import { AddCompanyFormData } from "@/entiy/form/AddCompanyFormData.ts";
import RequestService from "@/service/RequestService";
import { OrganizationInfoVO } from "@/entiy/vo/OrganizationInfoVO.ts";
import { BizError } from "@/common/errors/BizError";
import PromiseUtils from "@/utils/PromiseUtils";

export const useAddCompanyStore = defineStore("addCompanyStore", {
    state: () => {
        return {
            // 是否显示弹窗
            show: false,

            // 弹窗是否加载中
            loading: false,

            // 表单数据
            formData: {} as AddCompanyFormData,
        }
    },

    getters: {},

    actions: {
        // 添加新机构信息
        async addNewCompany(data: AddCompanyFormData): Promise<OrganizationInfoVO> {
            const params = {
                orgName: data.orgName,
                orgSimpleName: data.orgSimpleName,
                phone: data.phone,
                email: data.email,
            } as OrganizationInfoVO

            const result = await RequestService.post<OrganizationInfoVO>("add-organization-info", params);
            if (result.isError) {
                throw new BizError(result.errorMsg);
            }

            return PromiseUtils.resolveValue(result.bizDta);
        }
    }
});